<?php

namespace application\admin\controller;

use ticky\controller;
use ticky\request;
use ticky\response;
use ticky\captcha;
use \application\common\models\member_model;

class login extends controller {

    public function __construct() {

    }

    public function index() {
        global $_G;
        $user_id = member_model::get_user_id();
        if ($user_id) {
            $adminid = session('adminid');
            if ($adminid == 1) {
                response::redirect('/admin/index/index');
            } else {
                response::redirect('/admin/login/index');
            }
        } elseif (request::isPost()) {
            captcha::instance()->check_code();
            $username = isset($_POST['username']) && is_username($_POST['username']) ? trim($_POST['username']) : showmsg(L('user_name_format_error'));
            $password = isset($_POST['password']) && is_password($_POST['password']) ? trim($_POST['password']) : showmsg(L('password_format_error'));

            //电子邮箱和用户名两种登录方式
            $where = is_email($username) ? array('email' => $username) : array('username' => $username);
            $member = db('member');
            $user = $member->where($where)->find();
            if (!$user) {
                showmsg('该用户不存在!', '/admin/login/index');
            }
            if (authcode($user['password']) != $password) {
                showmsg('密码错误！', '/admin/login/index', 3);
            }
            if ($user['adminid'] == 1) {
                if ($user['status'] == '0')
                    showmsg('用户未通过审核！', '/admin/login/index', 'stop');
                else if ($user['status'] == '2')
                    showmsg('用户已锁定！', '/admin/login/index', 'stop');
                else if ($user['status'] == '3')
                    showmsg('用户已被管理员拒绝！', '/admin/login/index', 'stop');

                $roleMember = db('auth_role_member')->where(['uid' => $user['uid']])->find();
                if (count($roleMember) > 0) {
                    session('roleid', $roleMember['roleid']);
                } else {
                    session('roleid', 0);
                }
                session('uid', $user['uid']);
                session('username', $user['username']);
                session('groupid', $user['groupid']);
                session('adminid', $user['adminid']);

                $where = '`lastloginip`="' . getip() . '",`lastlogintime`="' . TIMESTAMP . '",`loginnum`=`loginnum`+1,`experience`=`experience`+1';
                $sql = "update tky_member set " . $where . " where uid=" . $user['uid'];
                db('member')->update($where, array('uid' => $user['uid']));
                showmsg('登录成功!', '/admin/index/index');
            } else {
                showmsg('抱歉，您不是后台用户,请联系管理员!', '/admin/login/index');
            }
        } else {
            $verifyCode = session('verify_code_login');
            $this->session('code', $verifyCode);
            $this->display('login');
        }
    }

}
